var evernote_cfg = require('./../evernote-cfg.json');
var evernote = require('evernote').Evernote;
var client = new evernote.Client({
    token: evernote_cfg.production.token,//申请的开发者token，正式环境使用evernote_cfg.production.token,沙盒使用evernote_cfg.develop.token
    sandbox: evernote_cfg.evernoteNotUseSandbox,//是否使用沙盒，正式上线时否
    serviceHost: "app.yinxiang.com" //印象笔记中国使用app.yinxiang.com,evernote使用www.evernote.com
});
var noteStore = client.getNoteStore();
var userStore = client.getUserStore();
/*
 * 列出用户信息
 * */
userStore.getUser(function (err, user) {
    // run this code
    console.log(user);
    console.log(user.isBusinessUser());
});


/*
 * 列出笔记本
 * */

var xx = {};
noteStore.listNotebooks(function (err, notebooks) {

    if (err) throw err;
    for(i = 0;i < notebooks.length;i++) {
        if (notebooks[i].name == "阅读队列") {
            xx = notebooks[i];
            console.log(xx);
        }
    }
});



/*
 * 创建笔记本
 * */
notebook = new evernote.Notebook();
notebook.name = "haha";

noteStore.createNotebook(notebook, function (err, createdNotebook) {
    if (err) {
        throw err;
    }

});

/*
 * 创建笔记
 * */
var noteBody = "wechat wechat";
var nBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
nBody += "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">";
nBody += "<en-note>" + noteBody + "</en-note>";

// Create note object
var ourNote = new evernote.Note();
ourNote.title = "wechat";
ourNote.content = nBody;

// parentNotebook is optional; if omitted, default notebook is used
ourNote.notebookGuid = "503afea4-d37e-499c-ae63-af43c3c6a983";


// Attempt to create note in Evernote account
noteStore.createNote(ourNote, function (err, note) {
    if (err) {
        // Something was wrong with the note data
        // See EDAMErrorCode enumeration for error code explanation
        // http://dev.evernote.com/documentation/reference/Errors.html#Enum_EDAMErrorCode
        console.log(err);
    }
});

/*
 * 查询笔记
 * */
var noteFilter = new evernote.NoteFilter;

noteFilter.words = '';//intitle:yorkie
noteFilter.order = evernote.NoteSortOrder[('CREATED')];
noteFilter.ascending = false;
noteFilter.inactive = false;

var offset = 0;
var count = 10;
var spec = new evernote.NotesMetadataResultSpec;
//spec.includeTitle = True;

noteStore.findNotes(evernote_cfg.production.token, noteFilter, offset, count, spec,
    function (err, notes) {

        if (err)  return console.log(err);

        return console.log(notes);
    }
);

/*
 * 获取笔记内容
 * */
noteStore.getNote(evernote_cfg.production.token, "71ebf7f7-beed-429d-941d-28a083a520f0", true, true, false, false,
    function (err, note) {

        if (err)  return console.log(err);

        return console.log(note);
    });


/*
 * 通过链接分享笔记
 * */
function shareSingleNote(noteStore, userStore, noteGuid, callback) {

    // Share a single note and return the public URL for the note
    var shardId;

    getUserShardId(userStore, function (data) {
        shardId = data;
    });


    noteStore.shareNote(noteGuid, function (shareKey) {
        if (shareKey.errorCode) {
            console.log("Error sharing note:");
            console.log(shareKey);
        } else {
            callback(EN_URL + "/shard/" + shardId + "/sh/" + noteGuid + "/" + shareKey);
        }
    });

}

function getUserShardId(userStore, callback) {

    // Get the User from userStore and return the user's shard ID

    userStore.getUser(function (user) {
        if (user.errorCode) {
            console.log("Exception while getting user's shardID:");
            console.log(user);
        } else {
            callback(user.shardId);
        }
    });

}
